package edu.pitt.cs.zhangfan.sa.review;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;

import edu.pitt.cs.zhangfan.sa.model.Review;
import edu.pitt.cs.zhangfan.sa.model.ReviewCorpus;
import edu.pitt.cs.zhangfan.sa.model.ReviewDocument;
import edu.pitt.cs.zhangfan.sa.model.Reviewer;

public class StatisticAnalyze {
	public void analyze(ReviewCorpus v1, ReviewCorpus v2, String path) throws IOException {
		File f = new File(path);
		String name = f.getName();
		name = name.substring(0,name.length()-4);
		ReviewDocument src = v1.getReviewDoc(name);
		ReviewDocument dst = v2.getReviewDoc(name);
		
		int numOfComments = 0;
		Hashtable<String, Integer> commentTypes = new Hashtable<String,Integer>();
		Hashtable<String, Integer> authorComments = new Hashtable<String,Integer>();
		
		
		ArrayList<String> sequence = new ArrayList<String>();
		BufferedReader reader = new BufferedReader(new FileReader(path));
		String line = reader.readLine();
		while(line!=null) {
			sequence.add(line.trim());
			line = reader.readLine();
		}
		System.out.println(name+getSurfaceRatio(sequence));
	}
	
	public String getSurfaceRatio(ArrayList<String> sequences) {
		int surface = 0;
		int text = 0;
		for(int i = 0;i<sequences.size();i++) {
			String tag = sequences.get(i);
			if(tag.equals("MODIFIED")) {
				surface++;
			} else if(tag.equals("ADD")||tag.equals("DELETE")){
				text++;
			}
		}
		double ratio = surface*1.0 /text;
		return "\t"+ratio + "\t "+surface+"\t "+text+"\t "+(surface+text);
	}
	
	public static ReviewCorpus getCorpus(ArrayList<ReviewCorpus> rc,String corpusName) {
		for(int i = 0;i<rc.size();i++) {
			if(rc.get(i).getCorpusName().trim().equals(corpusName)) {
				return rc.get(i);
			}
		}
		return null;
	}
	
	public int getTotalReviewLength(Reviewer rv) {
		ArrayList<Review> reviews = rv.getReviews();
		int totalLength = 0;
		for(int i = 0;i<reviews.size();i++) {
			totalLength += reviews.get(i).getComment().split(" ").length;
		}
		return totalLength;
	}
	
	public void getReviewDistributions(ReviewCorpus train) {
		Hashtable<String,Integer> reviews = new Hashtable<String,Integer>();
		ArrayList<ReviewDocument> docs  = train.getDocs();
		for(int i = 0;i<docs.size();i++) {
			ReviewDocument rd = docs.get(i);
			ArrayList<Reviewer> reviewers = rd.getReviewers();
			for(int j = 0;j<reviewers.size();j++) {
				Reviewer rv = reviewers.get(j);
				ArrayList<Review> rvs = rv.getReviews();
				for(int k = 0;k<rvs.size();k++) {
					String commentName = rvs.get(k).getShortCommentName();
					if(!reviews.containsKey(commentName)) {
						reviews.put(commentName, 1);
					} else {
						reviews.put(commentName, reviews.get(commentName)+1);
					}
				}
			}
		}
		
		Iterator<String> it = reviews.keySet().iterator();
		while(it.hasNext()) {
			String comment = it.next();
			System.out.println(comment+":"+reviews.get(comment));
		}
	}
	
	public void getReviewContributions(ReviewCorpus train) {
		Hashtable<String,Integer> reviews = new Hashtable<String, Integer>();
		Hashtable<String,Integer> reviewCnts = new Hashtable<String,Integer>();
		Hashtable<String,Integer> reviewLength = new Hashtable<String,Integer>();
		
		ArrayList<ReviewDocument> docs  = train.getDocs();
		for(int i = 0;i<docs.size();i++) {
			ReviewDocument rd = docs.get(i);
			ArrayList<Reviewer> reviewers = rd.getReviewers();
			for(int j = 0;j<reviewers.size();j++) {
				Reviewer rv = reviewers.get(j);
				String rvName = rv.getReviewerName();
				if(!reviews.containsKey(rvName)) {
					reviews.put(rvName,rv.getReviews().size());
				} else {
					reviews.put(rvName, reviews.get(rvName)+rv.getReviews().size());
				}
				if(!reviewCnts.containsKey(rvName)) {
					reviewCnts.put(rvName, 1);
				} else {
					reviewCnts.put(rvName, reviewCnts.get(rvName)+1);
				}
				int totalLength = getTotalReviewLength(rv);
				if(!reviewLength.containsKey(rvName)) {
					reviewLength.put(rvName,totalLength);
				} else {
					reviewLength.put(rvName, reviewLength.get(rvName)+totalLength);
				}
				
				
			}
		}
	
		Iterator<String> it = reviewCnts.keySet().iterator();
		while(it.hasNext()) {
			String name = it.next();
			double avgLength = reviewLength.get(name)*1.0/reviews.get(name);
			System.out.println(name+"\t"+reviews.get(name)+"\t"+avgLength+"\t"+reviewCnts.get(name));
		}
		
	}
	

	
	public static void main(String[] args) throws IOException, CloneNotSupportedException {
		StatisticAnalyze sa = new StatisticAnalyze();
		ReviewReader rr = new ReviewReader();
		ArrayList<ReviewCorpus> rc = rr
				.readCorpus("E:/independent study/sentence alignment/peerComments.xlsx");
		ReviewCorpus train1 = getCorpus(rc, "P2D1");
		ReviewCorpus train2 = getCorpus(rc,"P2D2");
		ReviewCorpus test1 = getCorpus(rc,"P3D1");
		ReviewCorpus test2 = getCorpus(rc,"P3D2");
		
		String trainPath = "E:\\independent study\\sentence alignment\\align_annotation\\train";
		String testPath  = "E:\\independent study\\sentence alignment\\align_annotation\\test";
		
		File f = new File(testPath);
		File[] subs = f.listFiles();
		for(int i = 0;i<subs.length;i++) {
			sa.analyze(train1, train2, subs[i].getAbsolutePath());
		}
		
		sa.getReviewContributions(train1);
		sa.getReviewDistributions(test1);
	}
}
